/*   1:    */ package tt.dz.common;
/*   2:    */ 
/*   3:    */ import java.io.PrintStream;
/*   4:    */ import java.io.UnsupportedEncodingException;
/*   5:    */ import java.security.Key;
/*   6:    */ import javax.crypto.Cipher;
/*   7:    */ import javax.crypto.KeyGenerator;
/*   8:    */ import javax.crypto.SecretKey;
/*   9:    */ import javax.crypto.SecretKeyFactory;
/*  10:    */ import javax.crypto.spec.DESKeySpec;
/*  11:    */ import org.apache.commons.codec.binary.Base64;
/*  12:    */ import org.apache.commons.codec.digest.DigestUtils;
/*  13:    */ 
/*  14:    */ public class DzDigester
/*  15:    */ {
/*  16:    */   private static final String DEFAULT_CHARSET = "UTF-8";
/*  17:    */   public static final String KEY_ALGORITHM = "DES";
/*  18:    */   public static final String CIPHER_ALGORITHM_ECB = "DES";
/*  19:    */   public static final String ENCRYPT_CDKEY = "QK7VoW2hFsE=";
/*  20:    */   
/*  21:    */   public static final String md5ForUser(String res, String rander)
/*  22:    */   {
/*  23:    */     try
/*  24:    */     {
/*  25: 44 */       return DigestUtils.md5Hex(DigestUtils.md5Hex(res.getBytes("UTF-8")) + rander);
/*  26:    */     }
/*  27:    */     catch (UnsupportedEncodingException e)
/*  28:    */     {
/*  29: 46 */       LogUtil.error("md5加密异常", e);
/*  30:    */     }
/*  31: 47 */     return null;
/*  32:    */   }
/*  33:    */   
/*  34:    */   public static final String sha1ForOper(String res)
/*  35:    */     throws Exception
/*  36:    */   {
/*  37:    */     try
/*  38:    */     {
/*  39: 53 */       return DigestUtils.sha1Hex(res.getBytes("UTF-8"));
/*  40:    */     }
/*  41:    */     catch (Exception e)
/*  42:    */     {
/*  43: 55 */       LogUtil.error("sha1加密异常", e);
/*  44: 56 */       throw e;
/*  45:    */     }
/*  46:    */   }
/*  47:    */   
/*  48:    */   private static String generateKey()
/*  49:    */     throws Exception
/*  50:    */   {
/*  51: 68 */     KeyGenerator keyGenerator = KeyGenerator.getInstance("DES");
/*  52: 69 */     keyGenerator.init(56);
/*  53: 70 */     SecretKey secretKey = keyGenerator.generateKey();
/*  54: 71 */     return Base64.encodeBase64String(secretKey.getEncoded());
/*  55:    */   }
/*  56:    */   
/*  57:    */   private static Key toKey(String key)
/*  58:    */     throws Exception
/*  59:    */   {
/*  60: 82 */     DESKeySpec des = new DESKeySpec(Base64.decodeBase64(key));
/*  61: 83 */     SecretKeyFactory keyFactory = SecretKeyFactory.getInstance("DES");
/*  62: 84 */     SecretKey secretKey = keyFactory.generateSecret(des);
/*  63: 85 */     return secretKey;
/*  64:    */   }
/*  65:    */   
/*  66:    */   public static String desEncrypt(String data, String key)
/*  67:    */     throws Exception
/*  68:    */   {
/*  69: 96 */     Key k = toKey(key);
/*  70: 97 */     Cipher cipher = Cipher.getInstance("DES");
/*  71: 98 */     cipher.init(1, k);
/*  72: 99 */     return Base64.encodeBase64String(cipher.doFinal(data.getBytes("UTF-8")));
/*  73:    */   }
/*  74:    */   
/*  75:    */   public static String desDecrypt(String data, String key)
/*  76:    */     throws Exception
/*  77:    */   {
/*  78:109 */     Key k = toKey(key);
/*  79:110 */     Cipher cipher = Cipher.getInstance("DES");
/*  80:111 */     cipher.init(2, k);
/*  81:112 */     return new String(cipher.doFinal(Base64.decodeBase64(data)));
/*  82:    */   }
/*  83:    */   
/*  84:    */   public static String encryptCommonInfo(String resoureInfo)
/*  85:    */   {
/*  86:    */     try
/*  87:    */     {
/*  88:122 */       return desEncrypt(resoureInfo, "QK7VoW2hFsE=");
/*  89:    */     }
/*  90:    */     catch (Exception e)
/*  91:    */     {
/*  92:124 */       e.printStackTrace();
/*  93:    */     }
/*  94:126 */     return null;
/*  95:    */   }
/*  96:    */   
/*  97:    */   public static String decryptCommonInfo(String encodeInfo)
/*  98:    */   {
/*  99:    */     try
/* 100:    */     {
/* 101:136 */       return desDecrypt(encodeInfo, "QK7VoW2hFsE=");
/* 102:    */     }
/* 103:    */     catch (Exception e)
/* 104:    */     {
/* 105:138 */       e.printStackTrace();
/* 106:    */     }
/* 107:140 */     return null;
/* 108:    */   }
/* 109:    */   
/* 110:    */   public static void main(String[] args)
/* 111:    */     throws Exception
/* 112:    */   {
/* 113:144 */     String res = "123456";
/* 114:145 */     String key = generateKey();
/* 115:146 */     System.out.println("key:" + key);
/* 116:147 */     String en = desEncrypt(res, key);
/* 117:148 */     System.out.println("密文：" + DigestUtils.md5Hex(res.getBytes("UTF-8")));
/* 117:148 */     System.out.println("密文：" + DigestUtils.md5Hex(res.getBytes("UTF-8")));
///* 118:149 */     String de = desDecrypt("7c4a8d09ca3762af61e59520943dc26494f8941b", key);
///* 118:149 */     String de = desDecrypt(en, key);
///* 119:150 */     System.out.println("解密：" + de);
///* 120:151 */     System.out.println("解密是否成功：" + de.equals(res));
/* 121:    */    
/* 122:    */ 
/* 123:154 */     System.out.println("name=" + encryptCommonInfo("123456"));
/* 124:155 */     System.out.println(decryptCommonInfo("MBYpuT27U/usFJYZwWKayA=="));
/* 124:155 */     System.out.println(decryptCommonInfo("88pKOU7tsqPSHwWdeuy7Zg=="));
///* 124:155 */     System.out.println(decryptCommonInfo(encryptCommonInfo("")));
System.out.println( DzDigester.sha1ForOper("123456"));
/* 125:    */   }
/* 126:    */ }


/* Location:           F:\work\pengyu\mainServer\WEB-INF\classes\
 * Qualified Name:     tt.dz.common.DzDigester
 * JD-Core Version:    0.7.0.1
 */